<?php

namespace app\admin\controller;

use app\admin\model\CategoryModel;
use app\admin\model\RoleModel;
use think\Request;

class Category extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        if(request()->isAjax()){
            $param = input('param.');
            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;
            if (!empty($param['searchText'])) {
                $where['cate_name'] = ['like', '%' . $param['searchText'] . '%'];
            }
//            $user = new RoleModel();
//            $selectResult = $user->getRoleByWhere($where, $offset, $limit);
            $cate=new CategoryModel();
            $status = config('cate_status');
            $where='pid = 0';
            $catelist=$cate->getcatelist($where,$limit,$offset);
            foreach($catelist as $key=>$vo){
                $catelist[$key]['operate'] = showOperate($this->makeButton($vo['id']));
                $catelist[$key]['status'] = $status[$vo['status']];
            }
            $return['total'] = $cate->getAllCate($where);  // 总数据
            $return['rows'] = $catelist;
            return json($return);
        }
        return $this->fetch('category/index');
    }

    //添加分类
    public function cateAdd()
    {
        if(request()->isPost()){
            Request::instance()->param();
            $param = input('post.');
            $param['pid']='0';
            $param['path']='0,';
            $cate = new CategoryModel();
            $flag = $cate->insertCate($param);
            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }

        $role = new RoleModel();
        $this->assign([
            'role' => $role->getRole(),
            'status' => config('cate_status'),
            'pcate' =>''

        ]);

        return $this->fetch('category/cateadd');
    }

    //修改分类
    public function cateEdit()
    {
        $cate = new CategoryModel();

        if(request()->isPost()) {
            $param = input('post.');
            $flag = $cate->editCate($param);
            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }
        $id = input('param.id');
        $this->assign([
            'cate' => $cate->getOneCate($id),
            'status' => config('cate_status'),
        ]);
        return $this->fetch('category/cateedit');

    }
    
    
    //删除分类
    public function cateDel()
    {
        $id = input('param.id');
        $cate = new CategoryModel();
        $flag = $cate->delCate($id);
        return json(msg($flag['code'], $flag['data'], $flag['msg']));
    }

    //添加子分类
    public function catechildAdd()
    {
        if(request()->isPost()){
            $param = input('param.');
            $param['path']='0,'.$param['pid'].',';
            $cate = new CategoryModel();
            $flag = $cate->insertCate($param);
            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }
        $cate = new CategoryModel();
        $pid=input('param.id');
        $role = new RoleModel();
        $this->assign([
            'role' => $role->getRole(),
            'status' => config('cate_status'),
            'pcate' =>$cate->getCatePid($pid)
        ]);
        return $this->fetch('category/cateadd');
    }

    //查看子分类
    public function cateChild()
    {
        $param = input('param.');
        if (request()->isAjax()){
            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;
            if (!empty($param['searchText'])) {
                $where['cate_name'] = ['like', '%' . $param['searchText'] . '%'];
            }
//            $user = new RoleModel();
//            $selectResult = $user->getRoleByWhere($where, $offset, $limit);
            $cate=new CategoryModel();
            $status = config('cate_status');
            $where='pid ='.$param['id'];
            $catelist=$cate->getcatelist($where,$limit,$offset);
            foreach($catelist as $key=>$vo){
                $catelist[$key]['operate'] = showOperate($this->makeChildButton($vo['id']));
                $catelist[$key]['status'] = $status[$vo['status']];
            }
            $return['total'] = $cate->getAllCate($where);  // 总数据
            $return['rows'] = $catelist;
            return json($return);
        }

        return view('category/catechild',['pid'=>$param['id']]);
    }

    //删除子分类
    public function cateChildDel()
    {
        $id = input('param.id');
        $cate = new CategoryModel();
        $flag = $cate->delCate($id);
        return json(msg($flag['code'], $flag['data'], $flag['msg']));
    }
    
    //编辑子分类
    public function catechildEdit()
    {
        $cate = new CategoryModel();
        if(request()->isPost()) {
            $param = input('post.');
            $flag = $cate->editCate($param);
            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }
        $id = input('param.id');
        $this->assign([
            'cate' => $cate->getOneCate($id),
            'status' => config('cate_status'),
        ]);
        return $this->fetch('category/cateedit');
    }
    /**
     * 拼装分类操作按钮
     * @param $id
     * @return array
     */
    private function makeButton($id)
    {
        return [
            '编辑' => [
                'auth' => 'category/cateedit',
                'href' => url('admin/Category/cateEdit', ['id' => $id]),
                'btnStyle' => 'primary',
                'icon' => 'fa fa-paste'
            ],
            '删除' => [
                'auth' => 'category/catedel',
                'href' => "javascript:cateDel(" .$id .")",
                'btnStyle' => 'danger',
                'icon' => 'fa fa-trash-o'
            ],
            '新增子分类' => [
                'auth' => 'category/catechildadd',
                'href' => url('admin/Category/catechildAdd', ['id' => $id]),
                'btnStyle' => 'info',
                'icon' => 'fa fa-plus-circle'
            ],
            '查看子分类' => [
                'auth' => 'category/catechild',
                'href' => url('admin/Category/catechild', ['id' => $id]),
                'btnStyle' => 'info',
                'icon' => 'fa fa-search'
            ],
        ];
    }

    /**
     * 拼装子分类操作按钮
     * @param $id
     * @return array
     */
    private function makeChildButton($id)
    {
        return [
            '编辑' => [
                'auth' => 'category/catechildedit',
                'href' => url('admin/Category/cateChildEdit', ['id' => $id]),
                'btnStyle' => 'primary',
                'icon' => 'fa fa-paste'
            ],
            '删除' => [
                'auth' => 'category/catechilddel',
                'href' => "javascript:cateChildDel(" .$id .")",
                'btnStyle' => 'danger',
                'icon' => 'fa fa-trash-o'
            ],
        ];
    }
}
